package pl.olap.persistence;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import pl.olap.dao.Genre;
import pl.olap.dao.Movie;
import pl.olap.dao.Person;
import pl.olap.dao.Production;

public class SaveEntityUtil {

	// private static Logger logger = LoggerFactory
	// .getLogger(SaveEntityUtil.class);

	public static void save(Object o) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		session.save(o);
		session.getTransaction().commit();
	}

	public static Person getPersonFromDB(String firstName, String lastName,
			Session session) {
		// Session session = HibernateUtil.getSessionFactory().openSession();
		// session.beginTransaction();
		Criteria crit = session.createCriteria(Person.class);
		crit.add(Restrictions.ilike("firstName", firstName.toLowerCase()));
		crit.add(Restrictions.ilike("lastName", lastName.toLowerCase()));
		List<Person> pList = crit.list();
		// session.getTransaction().commit();
		// session.close();
		if (pList != null && !pList.isEmpty()) {
			return pList.get(0);
		}
		return null;

	}

	public static synchronized void saveMovie(Movie m) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
//		List<Person> personList = new ArrayList<Person>();
//		if (m.getPersons() != null) {
//			for (Person per : m.getPersons()) {
//				Person p = Cache.getInstance().getPerson(per, session);
//				personList.add(p);
//			}
//			m.setPersons(personList);
//		}
		List<Genre> genreList = new ArrayList<Genre>();
		if (m.getGenres() != null) {
			for (Genre gen : m.getGenres()) {
				Genre g = Cache.getInstance().getGenre(gen);
				genreList.add(g);
			}
			m.setGenres(genreList);
		}
		
		List<Production> productionList = new ArrayList<Production>();
		if (m.getProductions() != null) {
			for (Production pro : m.getProductions()) {
				Production p = Cache.getInstance().getProduction(pro);
				productionList.add(p);
			}
			m.setProductions(productionList);
		}
		try {
			
			for (Genre g : m.getGenres()) {
				session.saveOrUpdate(g);
			}
			
			Map<String, String> personMap = new HashMap<String, String>();
			for (Person p : m.getPersons()) {
				String keyy = p.getFirstName()+"_"+p.getLastName();
				if (p != null && !personMap.containsKey(keyy)) {
					personMap.put(keyy, keyy);
					session.saveOrUpdate(p);
				}
			}
			
			for (Production p : m.getProductions()) {
				if (p != null) {
					session.saveOrUpdate(p);
				}
			}
			
			session.save(m);
			session.saveOrUpdate(m.getPremiere());
			
			session.getTransaction().commit();
			session.close();
			Cache.getInstance().addGenresToCache(m.getGenres());
			Cache.getInstance().addProductionsToCache(m.getProductions());
			// Cache.getInstance().addPersonsToCache(m.getPersons());
			// logger.debug("Zapisano obiekt");
			System.out.println("Zapisano film");
		} catch (Exception e) {
			// logger.error("Save entity problem", e);
			System.out.println("Problem z zapisem filmu. " + e.getMessage());
			e.printStackTrace();
		}

	}
}
